#!/bin/bash
shopt -s nocasematch

lovaid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=LoVa-service"|jq .[].id`
lova=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$lovaid/projects"?per_page=300|jq .[].name`
csid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=comm-service"|jq .[].id`
cs=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$csid/projects"?per_page=300|jq .[].name`
micid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=micro-app"|jq .[].id`
mic=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$csid/projects"?per_page=300|jq .[].name`
nfseid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=FSE"|jq .[].id`
nfse=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$nfseid/projects"?per_page=300|jq .[].name`
oms4bid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=backend"|jq .[].id`
oms4b=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$oms4bid/projects"?per_page=300|jq .[].name`
oms4fid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups?search=frontend"|jq .[].id`
oms4f=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/groups/$oms4fid/projects"?per_page=300|jq .[].name`
oms4=order,accounting,auth,fastdfs,global-setting,inhouse-fulfillment,integration,master,onboarding,openapi,report-master,report-order,scheduler,support,web,yapi

#for i in `kubectl get pod -A|grep -v "kube-system\|rook-ceph\|public-service\|monitoring\|logging\|istio-system\|cattle-system\|cattle-fleet-system\|default"|grep 1/2|awk '{print $1"_"$2"_"$5}'`
for i in `kubectl get pod -A|grep -v "kube-system\|rook-ceph\|public-service\|monitoring\|logging\|istio-system\|cattle-system\|cattle-fleet-system\|default"|grep -v Completed|grep -e 1/2 -e 0/1|awk '{print $1"_"$2"_"$5}'`
do
  ns=`echo $i|awk -F '_' '{print $1}'`
  podname=`echo $i|awk -F '_' '{print $2}'`
  num=`echo $i|awk -F '_' '{print $3}'`
  deploymentname=`echo $podname|awk -F"-" '{ for (i = 1; i <= NF-2; i++) { printf "%s", $i; if (i < NF-2) printf "-" } printf "\n" }'`
  flag_a=`kubectl describe deployment $deploymentname -n $ns|grep -w Available|awk '{print $2}'`
  flag_p=`kubectl describe deployment $deploymentname -n $ns|grep -w Progressing|awk '{print $2}'`
  #if [[ $num -gt 2 && $flag_a == "False" ]];then
  #if [[ $num -gt 2 && $flag_p == "False" ]];then
  if { [ $flag_p == "False" ] || [ $flag_a == "False" ]; } && [ `echo $num` -gt 2 ]; then
     echo "restart $deploymentname with $ns"
     echo `date`
     kubectl scale deployment $deploymentname --replicas=0 -n $ns && \
     kubectl scale deployment $deploymentname --replicas=1 -n $ns
     countzz=`cat /tmp/restartpod`
     count=$((countzz+1))
     echo $count > /tmp/restartpod
     if [[ $count -gt 2 ]];then
      echo "stop $deploymentname with $ns"
      echo `date`
      kubectl scale deployment $deploymentname --replicas=0 -n $ns
      echo 0 > /tmp/restartpod
      ver=`kubectl describe deployment $deploymentname -n $ns|grep Image|awk -F : '{print $NF}'`
      echo "ver="$ver
      ######################################
      #project_id=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" http://10.202.16.73:8900/api/v4/projects?search=$deploymentname|jq '.[] | select(.name == "${deploymentname}") | .id'`
      PAGE=1
      PER_PAGE=100
      while true; do
        RESPONSE=$(curl --silent --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects?per_page=$PER_PAGE&page=$PAGE")
        if [ "$(echo $RESPONSE | jq '. | length')" -eq 0 ]; then
           break  
        fi

        if [[ " ${lova[@]} " =~ $deploymentname ]];then
           if [[ ! -z `echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "LoVa-service" and .name == $deploymentname)| .id'` ]];then
              echo "This is lova"
              project_id=`echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "LoVa-service" and .name == $deploymentname)| .id'`
              #echo "project_id="$project_id
           fi
        elif [[ " ${cs[@]} " =~ $deploymentname ]];then
           if [[ ! -z `echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "comm-service" and .name == $deploymentname)| .id'` ]];then
              echo "This is cs"
              project_id=`echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "comm-service" and .name == $deploymentname)| .id'`
              #echo "project_id="$project_id
           fi
        elif [[ " ${mic[@]} " =~ $deploymentname ]];then
           if [[ ! -z `echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "micro-app" and .name == $deploymentname)| .id'` ]];then
              echo "This is micro-app"
              project_id=`echo "$RESPONSE" |jq --arg deploymentname "$deploymentname" '.[] | select(.namespace.name == "micro-app" and .name == $deploymentname)| .id'`
              #echo "project_id="$project_id
           fi
        elif [[ $deploymentname == ms-* ]];then
              echo "This is nfse"
              ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "The ${podname} service at ${ns} environment Service abnormal!"|mail -s Microservice\ relese\ exception  zhang.zhe@y3.sg,jiannluen.kow@y3.sg"
        elif [[ " ${oms4b[@]} " =~ $deploymentname || " ${oms4f[@]} " =~ $deploymentname ]];then
        #elif [[ " ${oms4[*]} " == *" $deploymentname "* ]];then
              echo "This is oms4"
              ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "The ${podname} service at ${ns} environment Service abnormal!"|mail -s Microservice\ relese\ exception  zhang.zhe@y3.sg,kai.zhong@y3.sg"
        fi
        PAGE=$((PAGE + 1))
      done
      ###################################################
      echo "project_id="$project_id
      commitid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects/$project_id/repository/tags"|jq '.[] | select(.name |contains ("${ver}")) | .commit.short_id'`
      echo "commitid="$commitid
      if [[ -z $commitid ]];then
        piplineid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects/$project_id/pipelines"|jq '.[].id'|head -n 1`
      else
        piplineid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects/$project_id/pipelines"|jq --arg commitid "$commitid" '.[] | select(.sha |contains ($commitid)) | .id'`
      fi
      echo "piplineid="$piplineid
      userid=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/projects/$project_id/pipelines/$piplineid"|jq '.user.id'`
      echo "userid="$userid
      emailzz=`curl -s --header "PRIVATE-TOKEN: gp-FpycGRszVvWvtRccY" "http://10.202.16.73:8900/api/v4/users/$userid"|jq '.email'|awk -F'"' '{print $2}'`
      echo "emailzz="$emailzz
      ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "The ${podname} service at ${ns} environment Service abnormal!"|mail -s Microservice\ relese\ exception  zhang.zhe@y3.sg,$emailzz"
     fi
  fi
done

